//
//  JMBCouponAddChooseCell.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2019/8/8.
//  Copyright © 2019年 Lssyyy. All rights reserved.
//

#import "JMBCouponAddChooseCell.h"
#import "UIButton+JMExtension.h"
#import "NSString+JMExtension.h"
@implementation JMBCouponAddChooseCell


- (void)addUI{
    
    self.topSepView.sd_layout
    .leftEqualToView(self.contentView)
    .rightEqualToView(self.contentView)
    .heightIs(10)
    .topEqualToView(self.contentView);
    
    self.titleLabel.sd_layout
    .leftSpaceToView(self.contentView, 15)
    .topSpaceToView(_topSepView, 0)
    .heightIs(50);
    [self.titleLabel setSingleLineAutoResizeWithMaxWidth:0.4 *UI_SCREEN_WIDTH];
    
    self.rightButton.sd_layout
    .rightSpaceToView(self.contentView, 15)
    .heightIs(27)
    .centerYEqualToView(self.titleLabel);
    
    self.leftButton.sd_layout
    .rightSpaceToView(self.rightButton, 10)
    .heightIs(27)
    .centerYEqualToView(self.titleLabel);
}

- (void)setItem:(CouponFormItem *)item{
    _item = item;
    
    self.titleLabel.attributedText = item.attributedTitle;
    [self configButtonWithItem:item];
}

- (void)configButtonWithItem:(CouponFormItem *)item{
    NSString *leftTitle;
    NSString *rightTitle;
    if (item.itemType == CouponFormItemTypeChooseCouponType) {
        leftTitle = JMLocalStr(@"现金券");
        rightTitle = JMLocalStr(@"折扣券");
    }
    if (item.itemType == CouponFormItemTypeChooseIsPublish) {
        leftTitle = JMLocalStr(@"是");
        rightTitle = JMLocalStr(@"否");
    }
    CGFloat leftWidth = [leftTitle jm_sizeWithFont:_leftButton.titleLabel.font].width;
    CGFloat rightWidth = [rightTitle jm_sizeWithFont:_rightButton.titleLabel.font].width;
    _rightButton.sd_layout.widthIs(rightWidth+36);
    _leftButton.sd_layout.widthIs(leftWidth+36);
    [_leftButton setTitle:leftTitle forState:0];
    [_rightButton setTitle:rightTitle forState:0];
    if ([item.info isEqualToString:@"1"]) {
        _leftButton.selected = NO;
        _rightButton.selected = YES;
    }else{
        _leftButton.selected = YES;
        _rightButton.selected = NO;
    }
}


- (void)chooseButtonMethod:(UIButton *)sender{
    if (sender.isSelected) {
        return;
    }
    if (sender.tag == 0) {
        _leftButton.selected = YES;
        _rightButton.selected = NO;
    }else{
        _leftButton.selected = NO;
        _rightButton.selected = YES;
    }
    
    if (_cellSelectedLeftButtonCompletion) {
        _cellSelectedLeftButtonCompletion(_leftButton.isSelected);
    }
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textColor = UIColorHex(333333);
        [self.contentView addSubview:_titleLabel];
    }
    return _titleLabel;
}
- (UIView *)topSepView{
    if (!_topSepView) {
        _topSepView = [UIView new];
        _topSepView.backgroundColor = UIColorHex(F3F3F3);
        [self.contentView addSubview:_topSepView];
    }
    return _topSepView;
}

- (UIButton *)leftButton{
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:0];
        [_leftButton setTitleColor:UIColorHex(6DBE2F) forState:UIControlStateSelected];
        [_leftButton setBackgroundColor:UIColorHex(F5FCEF) forState:UIControlStateSelected];
        [_leftButton setTitleColor:UIColorHex(282828) forState:UIControlStateNormal];
        [_leftButton setBackgroundColor:UIColorHex(E2E2E2) forState:UIControlStateNormal];
        [_leftButton.titleLabel setFont:[UIFont systemFontOfSize:13]];
        _leftButton.layer.cornerRadius = 14.0;
        _leftButton.layer.masksToBounds = YES;
        _leftButton.selected = YES;
        [_leftButton addTarget:self action:@selector(chooseButtonMethod:) forControlEvents:UIControlEventTouchUpInside];
        _leftButton.tag = 0;
        [self.contentView addSubview:_leftButton];
    }
    return _leftButton;
}

- (UIButton *)rightButton{
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:0];
        [_rightButton setTitleColor:UIColorHex(6DBE2F) forState:UIControlStateSelected];
        [_rightButton setBackgroundColor:UIColorHex(F5FCEF) forState:UIControlStateSelected];
        [_rightButton setTitleColor:UIColorHex(282828) forState:UIControlStateNormal];
        [_rightButton setBackgroundColor:UIColorHex(E2E2E2) forState:UIControlStateNormal];
        [_rightButton.titleLabel setFont:[UIFont systemFontOfSize:13]];
        _rightButton.layer.cornerRadius = 14.0;
        _rightButton.layer.masksToBounds = YES;
        [_rightButton addTarget:self action:@selector(chooseButtonMethod:) forControlEvents:UIControlEventTouchUpInside];
        _rightButton.tag = 1;
        [self.contentView addSubview:_rightButton];
    }
    return _rightButton;
}
@end
